//app.js
App({
  onLaunch: function () {
    
    var that=this;
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    
      wx.login({
        success: function (response) {
          var code = response.code
          wx.getUserInfo({
            success: function (resp) {
              var uI=resp.userInfo;
              wx.request({
                url: 'http://soya.91soya.com/onLogin',
                data: {
                  code: code,
                  iv: resp.iv,
                  encryptedData: resp.encryptedData
                },
                success: function (res) {
                  that.globalData.openId=res.data.openId;
                  wx.request({
                    url: 'http://soya.91soya.com/api/wxcreate',
                    method:"post",
                    data:{
                      openid: res.data.openId,
                      avatarUrl: uI.avatarUrl,
                      country: uI.country,
                      city: uI.city,
                      gender: uI.gender,
                      language: uI.language,
                      nickName: uI.nickName,
                      province: uI.province
                    }
                  })
                }
              })
            }
          })
        },
        fail: function () {
      
        }
    });

    
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              // this.globalData.openId = res.

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
 
  globalData: {
    userInfo: null,
    openId: null
  },
  // public function
  Nav: function () {
    return function (a) {
      var Num = a.target.dataset.id;
      var Left = a.currentTarget.offsetLeft;
      // var Left = parseFloat(Num) * b * 100 + "%";
      this.setData({
        navIndex: "x" + Num,
        curIndex: "x" + Num,
        left: Left+"px"
      })
    }
  },
  NavgateTo:function(){
    return function(e){
      var id=e.target.dataset.id;
      var url=e.target.dataset.url;
      console.log(e)
      wx.navigateTo({
        url: "/pages/"+url+"/"+url+"?id="+id,
      })
    }
  },
  call:function(){
    return wx.makePhoneCall({
      phoneNumber: '021-3220-0089',
    })
  },
  load:function(){
    return function(a){
      var navL = Object.keys(a.data.nav).length;
      var query = wx.createSelectorQuery().in(a);
      for (var i = 0; i < navL; i++) {
        (function (b) {
          query.select("#x" + b).boundingClientRect(function (res) {
            that.setData({
              ["top" + b]: res.top,
            })
          })
        })(i)
      }
      query.select("#scrollBox").boundingClientRect(function (res) {
        for (var i = 0; i < navL; i++) {
          var h0 = that.data.top0 - res.top;
          
          (function () { 
            
          })()
        }
       
        var h1 = that.data.top1 - res.top;
        var h2 = that.data.top2 - res.top;
        this.setData({
          scrollT: res.top,
          h0: h0,
          h1: h1,
          h2: h2,
        })
      }).exec()
    }
  },
})